<?php
namespace app\logic\model;
use think\facade\Db;

class CollectionRank{
		
		
	public function selectBuyMoney($_where,$_field,$_group){
		$_table="(select * from ".config('database.connections.mysql.prefix')."order_opus where id in ( select max(id) from ".config('database.connections.mysql.prefix')."order_opus where status = 1 group by opus_number_id)"; 
		$_table.=" )";
		$_data=Db::name("opus_number")->alias("number")
			->join("opus opus","opus.id=number.opus_id")
			->leftJoin($_table." o","o.opus_number_id=number.id and o.status=1")
			->where($_where)
			->field($_field)
			->group($_group)
			->select();
		return empty($_data)?:$_data->toArray();
	}
	
	public function selectUserSkuType($_where,$_field,$_order,$_group){
		$_data=Db::name("opus_number")->alias("n")
				->join("nft_opus opus","opus.id=n.opus_id")
				->join("nft_opus_series s","s.id=opus.series_id")
				->where($_where)
				->field($_field)
				->order($_order)
				->group($_group)
				->select();	
		return empty($_data)?:$_data->toArray();	
	}
	
	public function insertCollectionRank($_data){
		return Db::name("collection_rank")->insert($_data,true);
	}
	
	public function findCollectionRank($_where,$_field,$_user_id,$_where_left_join=''){
		return Db::name("collection_rank")->alias("r")
				->join("user u","r.collection_id=u.id".$_where_left_join)
				->leftJoin("user_focus f","f.focus_id=u.id and f.user_id=".$_user_id)
				->where($_where)
				->field($_field)
				->find();
	}
	
	public function selectCollectionRank($_where,$_field,$_limit,$_order,$_user_id,$_where_left_join='')
	{
		$_data=Db::name("collection_rank")->alias("r")
				->join("user u","r.collection_id=u.id".$_where_left_join)
				->leftJoin("user_focus f","f.focus_id=u.id and f.user_id=".$_user_id)
				->where($_where)
				->field($_field)
				->limit(current($_limit),end($_limit))
				->order($_order)
				->select();
		return !empty($_data)?$_data->toArray():[];				
	}
	
	public function selectUserFocus($_where,$_field,$_group){
		$_data=Db::name("user_focus")->where($_where)->field($_field)->group($_group)->select();
		return !empty($_data)?$_data->toArray():[];
	}
	
	public function findCollectionData($_where,$_field,$_order="id asc"){
		return Db::name("collection_rank")->where($_where)->order($_order)->field($_field)->find();
	}
	
	public function insertSeriesPrice($_data){
		return Db::name("series_market_value")->insert($_data,true);
	}
	
	public function insertNowMoney($_data){
		return Db::name("rank_collection_now")->insert($_data,true);
	}
}
?>